package com.google.code.openperfmon.db;

import java.io.InputStream;
import java.io.StringWriter;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.XmlDataSet;
import org.dbunit.operation.DatabaseOperation;

public class DataUtil {
	private static String driver;
	private static String url;
	private static String testUrl;
	private static String user;
	private static String password;
	private static boolean initialized = false;
	
	static void dbSetup(boolean test) {
		try {
			if (initialized) return;
			
			Properties prop = DBManager.getProperties();
			driver = prop.getProperty("hibernate.connection.driver_class");
			url = prop.getProperty("hibernate.connection.url");
			testUrl = prop.getProperty("test.hibernate.connection.url");
			user = prop.getProperty("hibernate.connection.username");
			password = prop.getProperty("hibernate.connection.password");
			
			if (test) prop.setProperty("hibernate.connection.url", testUrl);
			DBManager.updateSchema();
			
			DriverManager.registerDriver((Driver)Class.forName(driver).newInstance());
			
			initialized = true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new IllegalStateException(e);
		}
	}
	
	private static IDatabaseConnection open(String anUrl) throws SQLException {
		return new DatabaseConnection(DriverManager.getConnection(anUrl, user, password));
	}
	
	public static void printDbContents(boolean main) throws Exception {
		IDatabaseConnection conn = open(main ? url : testUrl);
		try {
			IDataSet ds = conn.createDataSet();
			StringWriter w = new StringWriter();
			XmlDataSet.write(ds, w);
			System.out.println(w.toString());
		}
		finally {
			conn.close();
		}
	}
	
	public static void populate() throws Exception {
		populate("std-profile");
	}
	
	public static void populate(String profile) throws Exception {
		populate(profile, false);
	}
	
	public static void populate(String profile, boolean main) throws Exception {
		IDatabaseConnection conn = open(main ? url : testUrl);
		try {
			InputStream is = DataUtil.class.getResourceAsStream("/testdata/" + profile + ".xml");
			IDataSet ds = new XmlDataSet(is);
			DatabaseOperation.CLEAN_INSERT.execute(conn, ds);
			is.close();
		}
		finally {
			conn.close();
		}
	}
	
	public static void main(String[] args) throws Exception {
		dbSetup(true);
		if (args[0].equals("print-main")) {
			printDbContents(true);
		}
		else if (args[0].equals("print-test")) {
			printDbContents(false);
		}
		else if (args[0].equals("populate-main")) {
			String profile = args.length > 1 ? args[1] : "std-profile";  
			populate(profile, true);
		}
		else if (args[0].equals("populate-test")) {
			String profile = args.length > 1 ? args[1] : "std-profile";
			populate(profile, false);
		}
		else if (args[0].equals("export")) {
			String file = args.length > 1 ? args[1] : "target/schema.ddl";
			DBManager.exportSchema(file);
		}
	}
}
